package com.jtjrenren.android.taxi.passenger.manger.push.cmd;

import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderAmountResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OtherLoginResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.ReceiveConCmd;
import com.jtjrenren.android.taxi.passenger.bean.protocl.CommonResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderAcceptResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderDispatchResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderDriverCancleResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderDriverIncomeResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderFilterResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderPassgerIncarResponse;
import com.jtjrenren.android.taxi.passenger.utils.ModuleUtils;
import com.library.utils.ConvertUtils;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by Administrator on 2016/1/15.
 */
public class ReceiveControlCmd extends Protocl {
    /**
     * 是否为心跳回复
     *
     * @param cmd
     * @return
     */
    public static boolean isHeartCmd(String cmd) {
        ReceiveConCmd receiveCmd = parseReceiveCmd(cmd);
        if (receiveCmd == null)
            return false;
        if (MSG_ID_CENTER_RESPONSE.equals(receiveCmd.getMsg_id())) {
            CommonResponse commonResponse = parseCommonResponse(receiveCmd.getContent());
            if (MSG_ID_HEART.equals(commonResponse.getCmdId()))
                return true;
        }
        return false;
    }

    /**
     * 解析为通用回复
     *
     * @param content
     * @return
     */
    public static CommonResponse parseCommonResponse(String content) {
        CommonResponse response = null;
        if (content.length() == 18) {
            response = new CommonResponse();
            response.setSerialNo(Integer.parseInt(content.substring(0, 4), 16));
            response.setCmdId(content.substring(4, 8));
            response.setResult(content.substring(8, 10));
            response.setOrderSerialID(parseOrderSerialNo(content.substring(10)));
        }
        return response;
    }

    /**
     * 解析订单派发情况
     *
     * @param content
     */
    public static OrderDispatchResponse parseOrderDispatchResponse(String content) {
        OrderDispatchResponse response = null;
        if (content.length() == 24) {
            response = new OrderDispatchResponse();
            response.setOrderSerilNo(parseOrderSerialNo(content.substring(0, 8)));
            response.setDispatchDriverNum(Integer.parseInt(content.substring(8, 16), 16));
            response.setNotifyDriverNum(Integer.parseInt(content.substring(16), 16));
        }
        return response;
    }

    /**
     * 解析订单正在被筛选
     *
     * @param content
     * @return
     */
    public static OrderFilterResponse parseOrderFilterResponse(String content) {
        OrderFilterResponse response = null;

        if (content.length() == 8) {
            response = new OrderFilterResponse();
            response.setOrderSeriNo(parseOrderSerialNo(content.substring(0)));
        }

        return response;
    }

    /**
     * 解析订单接单情况
     *
     * @param content
     * @return
     */
    public static OrderAcceptResponse parseOrderAccepctResponse(String content) {
        OrderAcceptResponse response = null;

        if (content.length() >= 10) {
            response = new OrderAcceptResponse();
            response.setResult(Integer.parseInt(content.substring(0, 2), 16));
            response.setOrderSerialNo(parseOrderSerialNo(content.substring(2, 10)));
            if (content.length() > 10) {
                response.setDriverInfo(content.substring(10));
            }
        }

        return response;
    }

    /**
     * 解析司机正在接驾中
     *
     * @param content
     * @return
     */
    public static OrderDriverIncomeResponse parseOrderDriverIncomResponse(String content) {
        OrderDriverIncomeResponse response = null;
        if (content.length() == 8) {
            response = new OrderDriverIncomeResponse();
            response.setOrderSerialNo(parseOrderSerialNo(content.substring(0)));
        }
        return response;
    }

    /**
     * 解析司机确认乘客上车
     *
     * @param content
     * @return
     */
    public static OrderPassgerIncarResponse parseOrderPassgerIncarResponse(String content) {
        OrderPassgerIncarResponse response = null;
        if (content.length() == 8) {
            response = new OrderPassgerIncarResponse();
            response.setOrderSerialNo(parseOrderSerialNo(content.substring(0)));
        }
        return response;
    }

    /**
     * 解析司机取消订单
     *
     * @param content
     * @return
     */
    public static OrderDriverCancleResponse parseOrderDriverCancleResponse(String content) {
        OrderDriverCancleResponse response = null;

        if (content.length() >= 8) {
            response = new OrderDriverCancleResponse();
            response.setOrderSerialNo(parseOrderSerialNo(content.substring(0, 8)));
            if (content.length() - 8 - 2 > 0) {
                String substring = content.substring(8, content.length() - 2);
                try {
                    String gbk = new String(ConvertUtils.HexStrToByte(substring), "GBK");
                    response.setCancleDesc(gbk);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }

        return response;
    }

    /**
     * 对中心下发车费的解析
     *
     * @param content
     * @return
     */
    public static OrderAmountResponse parseOrderAmountResponse(String content) {
        OrderAmountResponse response = null;

        if (content.length() >= 20) {
            response = new OrderAmountResponse();
            response.setOrderSerialNo(parseOrderSerialNo(content.substring(0, 8)));
            response.setPayStatus(Integer.parseInt(content.substring(8, 10)));
            response.setUseTime(content.substring(10, 14));
            response.setTotalDistance(content.substring(14, 20));

            String substring = content.substring(20);
            List<String> strings = end00StringToArray(substring);
            String payAmuout = strings.get(0);
            String tipAmount = strings.get(1);
            try {
                payAmuout = new String(ConvertUtils.HexStrToByte(payAmuout), "GBK");
                tipAmount = new String(ConvertUtils.HexStrToByte(tipAmount), "GBK");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setPayAmount(ModuleUtils.DesDecrypt(payAmuout));
            response.setTipAmount(ModuleUtils.DesDecrypt(tipAmount));
            response.setPrePayNo(new String(ConvertUtils.HexStrToByte(strings.get(2))));
        }

        return response;
    }

    /**
     * 将协议进行初步解析
     * 1、检查该cmd是否是完整的，即是否两头都有标志位,可能存在的问题是连包现象，所以通过第一次和第二次的标志位进行截取
     * 2、检查是否达到最短长度要求
     * 3、并不进行校验码校验 但是要计算 留给下边处理
     *
     * @param cmd
     * @return 如果为null 则表示该信息不完整
     */
    public static ReceiveConCmd parseReceiveCmd(String cmd) {

        ReceiveConCmd receiveConCmd = null;

        int flagSize = PROTOCOL_FLAG.length();

        int[] cmdFlagIndex = ModuleUtils.getCmdFlagIndex(cmd);

        int fristIndexOf = cmdFlagIndex[0];
        int secondIndexOf = cmdFlagIndex[1];

        if (secondIndexOf + flagSize - fristIndexOf > PROTOCL_SIZE_MIN) {
            cmd = cmd.toUpperCase();
            //去除标志位
            cmd = cmd.substring(fristIndexOf + flagSize, secondIndexOf);
            //进行转义
            cmd = getTransReceiveProtocl(cmd);

            receiveConCmd = new ReceiveConCmd();
            receiveConCmd.setMsg_id(cmd.substring(0, 4));
            receiveConCmd.setAccount(parseAccountCmdStr(cmd.substring(4, 44)));
            receiveConCmd.setSerialNo(cmd.substring(44, 48));

            receiveConCmd.setContent(cmd.substring(48, cmd.length() - 2));
            receiveConCmd.setCheckCode(cmd.substring(cmd.length() - 2));

            receiveConCmd.setCheckCodeCalc(ConvertUtils.byteToHexStr(
                    getProtoclCheckCode(
                            ConvertUtils.HexStrToByte(cmd.substring(0, (cmd.length() - 2)))
                    )).toUpperCase());
        }
        return receiveConCmd;
    }

    /**
     * 解析其他设备登陆的命令
     *
     * @param content
     * @return
     */
    public static OtherLoginResponse parseOtherDutyOn(String content) {
        OtherLoginResponse response = null;
        if (content.length() > 2) {
            response = new OtherLoginResponse();
            String type = content.substring(0, 2);
            response.setType(type);
            List<String> stringList = end00StringToArray(content);
            if (stringList != null && stringList.size() > 0) {
                response.setDesc(cmdStringstr2String(stringList.get(0)));
            }
        }
        return response;
    }

}
